function changeMailsField() {
        var emails = '';
        $(".mailingCheckbox:checked").each(function(){
            if($(this).val() == 'user') {
                emails += userEmails;
            }
            else if($(this).val() == 'anonim') {
                if(emails) emails += ' ';
                emails += unregisterEmails;
            }   
            else {
                if(emails) emails += ' ';
                emails+= subscribeEmails;
            } 
        })
        var arr = emails.split(' ');
	console.log(arr);
        var uniqued = arr.filter(function(e,i,a){return i == a.indexOf(e)});
	
        emails = uniqued.join();
        if(emails.charAt(0) == ',') emails = emails.slice(1);
        $('.mails').val(emails);
    }    
    
    $(document).ready(
        function() {
            $('.mailingCheckbox').click(function(){
                changeMailsField();
            });
            
            $('#clearEmails').click(function() {
                $(".mailingCheckbox:checked").each(function(){
                    $(this).attr('checked', false);
                })
                $('.mails').val('');
            });
            
            $('.unregisterMails').click(function() {
                if ($('#unregisterMails').css("display") == 'none') {
                    $('#unregisterMails').css({'display':'block'}); 
                } else $('#unregisterMails').css({'display':'none'}); 
            });
            
            $('.subscribed').click(function() {
                if ($('#subscribed').css("display") == 'none') {
                    $('#subscribed').css({'display':'block'}); 
                } else $('#subscribed').css({'display':'none'}); 
            });
        }
    );    